package com.yang.service.Impl;

import com.yang.mapper.FoodMapper;
import com.yang.mapper.FoodTypeMapper;
import com.yang.mapper.SysUserMapper;
import com.yang.pojo.Food;
import com.yang.pojo.FoodType;
import com.yang.pojo.SysUser;
import com.yang.service.FoodService;
import com.yang.utils.Constants;
import com.yang.utils.JsonResult;
import com.yang.utils.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Created by 杨天可 on 2022/3/29 11:48
 */
@Service("foodService")
@Transactional
public class FoodServiceImpl implements FoodService {
    @Resource
    FoodMapper foodMapper;
    @Resource
    FoodTypeMapper foodTypeMapper;
    @Resource
    SysUserMapper sysUserMapper;

    @Override
    public Map<String, Object> save(Food food) {
        JsonResult jsonResult = new JsonResult();
        if(StringUtils.isEmpty(Objects.toString(food.getId()))){
            /* 新增*/
            foodMapper.insertSelective(food);
        }else {
            System.out.println(food.getImg());
            foodMapper.updateById(food);
        }

        jsonResult.setCode(Constants.SUCCESS);
        jsonResult.setMsg("操作成功");

        return jsonResult.getValues();
    }

    @Override
    public Map<String, Object> delFood(Integer foodId) {
        JsonResult jsonResult = new JsonResult();
        Food food = foodMapper.selectByPrimaryKey(foodId);
        food.setIsDel(Constants.YES);
        int i = foodMapper.updateByPrimaryKey(food);
        if (i > 0) {
            jsonResult.setCode(Constants.SUCCESS);
            jsonResult.setMsg("删除成功");
        } else {
            jsonResult.setCode(Constants.FAIL);
            jsonResult.setMsg("删除失败");
        }
        return jsonResult.getValues();
    }

    @Override
    public Map<String, Object> findAll(Integer userId,Integer foodTypeId, String name, Integer currentPage, Integer pageSize) {
        JsonResult jsonResult = new JsonResult();
        Integer startPage = (currentPage-1)*pageSize;

        SysUser byId = sysUserMapper.findById(userId);
        if (byId.getUsername().equals("管理员")){//管理员可查看所有数据
            List<Food> foodLimit = foodMapper.findByLimit(foodTypeId,name,startPage,pageSize);
            List<Food> foodList = foodMapper.findAll(foodTypeId,name);
            jsonResult.setMsg("获取成功");
            jsonResult.setCode(Constants.SUCCESS);
            jsonResult.setData(foodLimit);
            jsonResult.put("total",foodList.size());
        }else {
            List<Food> foodLimit = foodMapper.findByUserIdLimit(userId,foodTypeId,name,startPage,pageSize);
            List<Food> foodList = foodMapper.findByUserId(userId,foodTypeId,name);;
            jsonResult.setMsg("获取成功");
            jsonResult.setCode(Constants.SUCCESS);
            jsonResult.setData(foodLimit);
            jsonResult.put("total",foodList.size());
        }
        return jsonResult.getValues();
    }

    @Override
    public List<Food> findByType(String foodTypeId, Integer userId) {
        if (foodTypeId.equals("0")){
            return foodMapper.findBDisCountFood(userId);
        }else if(foodTypeId.trim().equals("")){
            List<FoodType> byUserId = foodTypeMapper.findByUserId(userId);
            if (byUserId.size() > 0){
                foodTypeId = byUserId.get(0).getId().toString();
            }
            return foodMapper.findByType(foodTypeId);
        }else {
            return foodMapper.findByType(foodTypeId);
        }
    }

    @Override
    public Food findFoodById(Integer foodId) {
        return foodMapper.findFoodById(foodId);
    }

}
